<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <!-- import CSS -->
  <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
</head>
<body>
<div id="app">
  <h1>发布微博页面</h1>
  <el-upload
          name="pic"
          action="/upload"
          list-type="picture-card"
          :on-success="handleSuccess"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove">
    <i class="el-icon-plus"></i>
  </el-upload>
  <el-dialog :visible.sync="dialogVisible">
    <img width="100%" :src="dialogImageUrl" alt="">
  </el-dialog>
  <input type="text" v-model="weibo.content" placeholder="说点儿什么...">
  <input type="button" value="发布微博" @click="send()">
</div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
  let v = new Vue({
    el: '#app',
    data: function () {
      return {
        dialogImageUrl: '',
        dialogVisible: false,
        weibo:{content:"",urls:""},
        arr:[]
      }
    },
    methods: {
      handleSuccess(response,file,fileList){
        //把上传成功的图片路径装进arr数组
        v.arr.push(response);
      },
      send(){
        //数组的toString()方法可以将数组中的多个字符串合并成一个字符串用,合并
        v.weibo.urls = v.arr.toString();
        axios.post("/weibo/insert",v.weibo).then(function (response) {
          if (response.data==1){
            alert("发布完成!");
            location.href="/";//回到首页
          }else {
            alert("请先登录")
            location.href="/login.html"
          }
        })
      },
      handleRemove(file, fileList) {
        /* let index = v.arr.indexOf(file.response) 先得到删除图片路径的位置 */
        //从arr数组中删除图片路径
        v.arr.splice(v.arr.indexOf(file.response,1));
        console.log(file, fileList);
        //发出删除的请求  file.response得到的是文件的路径
        console.log(file.response);
        axios.get("/remove?url="+file.response).then(function () {
          console.log("删除成功!");
        })
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogVisible = true;
      }
    }
  })
</script>
</html>